<template>
  <basic-dialog
    width="60%"
    title="选择客户"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCloseDialog"
  >
    <BasicForm
      :colNumOfRow="2"
      :isScalingBtn="false"
      :hasQueryPermission="false"
      :formColumns="formItemList"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      @getFormData="getFormData"
    />
    <BasicTable
      showActionCol
      :showSelectCol="false"
      :loading="tableInfo.loading"
      :columns="tableInfo.columns"
      :dataSource="tableInfo.list"
      :pagination="pagination"
      :pageSizes="pageSizes"
      :tableHeight="480"
      :actionRender="renderTableBtnAction"
      @pageChange="handlePageChange"
      @pageSizeChange="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import BasicTable from '@/components/BasicTable';
import basePage from '@/mixins/commodity/basePage';
import { getTableAction } from '@/utils/getBtnsRender';
import { queryCustomerListService } from '@/api/commodity/common';
import { PLATFORM_STATE } from '@/constants/commodity';

export default {
  mixins: [basePage],
  components: {
    BasicDialog,
    BasicForm,
    BasicTable
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formData: { searchParam: '' }, // 客户信息（名称或代码）
      formItemList: [{ label: '客户信息', prop: 'searchParam', type: 'input' }],
      tableInfo: {
        list: [],
        columns: [
          { title: '客户代码',  key: 'customerCode' },
          { title: '客户名称',  key: 'customerName' },
          { title: '业务员',  key: 'userName' },
          { title: '客户类型',  key: 'customerIndustry' },
          { title: '客户归属',  key: 'groupName' },
          { title: '客户等级',  key: 'customerStatus' },
          {
            title: '入驻平台',
            render: (h, { row }) => {
              return <span>{this.getPlatformInfo(row?.platformType)}</span>
            }
          }
        ],
        loading: false
      }
    }
  },
  methods: {
    // 渲染表格按钮
    renderTableBtnAction (h, { row }) {
      const actionList = [{
        label: '选择',
        method: this.handleSelect
      }];
      return getTableAction({ h, row, actionList })
    },
    // 获取表格数据
    async getTableData () {
      if (!this.formData.searchParam) return this.$message.warning('请输入筛选内容后再查询');
      const data = { ...this.formData, ...this.paginationParams };
      const loadingFn = state => { this.tableInfo.loading = state };
      const retData = await queryCustomerListService(data, loadingFn);
      if (retData) {
        this.tableInfo.list = retData?.result ?? [];
        this.pagination.totalNum = retData?.totalRow ?? 0;
      }
    },
    // 选择客户
    handleSelect (row) {
      this.$emit('getSelectResults', row);
      this.handleCloseDialog();
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
    // 获取入住平台信息
    getPlatformInfo (platformType) {
      if (!platformType) return '';
      const plattypes = platformType.split(',');
      const platformInfo = plattypes.reduce((value, item) => {
        const platState = PLATFORM_STATE[item]
        if (platState) value += `${platState}，`
        return value
      }, '')
      const result = platformInfo.slice(0, platformInfo.length - 1)
      return result
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog__header {
  border-bottom: 1px solid #e8eaec;
}
/* 分页样式 */
.pagination_wrap {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
